package Study0;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;




public class Study08 {
//    计算两个数值中的最大值，三个数值中的最大值的方法（重载）
    public static int max (int a, int b) {
        return a > b ? a : b;
    }

    public static int max (int a,int b, int c) {
        int max = a > b ? a : b;
        return max > c ? max : c;
    }

    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = 0;
        int y = 0;
        int z = 0;
        System.out.print("x的值:");
        if(scanner.hasNextInt()) {
            x = scanner.nextInt();
        }
        System.out.print("y的值:");
        if(scanner.hasNextInt()) {
            y = scanner.nextInt();
        }
        System.out.print("z的值:");
        if(scanner.hasNext()) {
            z = scanner.nextInt();
        }
        System.out.println("x ,y 中的最大值是:"+max(x,y));
        System.out.println("x,y,z中的最大值是:"+max(x,y,z));


        scanner.close(); // 关闭IO流，代码好习惯
    }


//    计算两个矩阵的和
    public static void addMatrix (int[][] a,int[][] b,int[][] c) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
    }


    public static void printMatrix (int[][] m) {
        for (int[] tmp:m) {
            for (int num:tmp) {
                System.out.print("\t"+num);
            }
        }
        System.out.println();
    }

    public static void main20 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        Study08 aM = new Study08();
        int[][] a = {{1,2,3},
                     {4,5,6},};
        int [][] b = {{6,3,4},
                      {5,1,2},};
        int[][] c = new int[2][3];
        aM.addMatrix(a,b,c);
        System.out.println("矩阵a");
        aM.printMatrix(a);
        System.out.println("矩阵b");
        aM.printMatrix(b);
        System.out.println("矩阵c");
        aM.printMatrix(c);

        scanner.close(); // 关闭IO流，代码好习惯
    }


//    判断两个数组是否是否相等
    public static boolean arrayEqual (int[] arr,int[] brr) {
        if(arr.length != brr.length) {
            return false;
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != brr[i]) {
                return false;
            }
        }
        return true;
    }
    public static void main19 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        Study08 AE = new Study08();
        System.out.print("数组arr中元素的个数：");
        int na = scanner.nextInt();
        int[] arr = new int[na];
        System.out.print("数组brr中元素的个数：");
        int nb = scanner.nextInt();
        int[] brr = new int[nb];
//        brr[1] = 1;
        System.out.println(AE.arrayEqual(arr,brr) ? "它们相等" : "它们不相等");

        scanner.close(); // 关闭IO流，代码好习惯

    }


//   将值读入列到数组元素中，并进行倒序排列
    public static void swap (int[] arr,int idx1,int idx2 ){
        int tmp = arr[idx1];
        arr[idx1] = arr[idx2];
        arr[idx2] = arr[idx1];
    }

    public static void reverse(int[] arr) {
        for (int j = 0; j < arr.length / 2; j++) {
            swap(arr,j,arr.length-j-1);
        }
    }
    public static void main18 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        Study08 re = new Study08();
        int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
        for (int tmp:arr) {
            System.out.print("\t"+tmp);
        }
        re.reverse(arr);
        System.out.println();
        for (int tmp:arr) {
            System.out.print("\t"+tmp);
        }

        scanner.close(); // 关闭IO流，代码好风格

    }


//    线性查找数组a的全部元素中最先与key值相同的元素
    public static int find (int[] arr,int key) {
        for (int i = 0; i < arr.length; i++) {
            if(key == arr[i]) {
                return i;
            }
        }
        return -1;
    }
    public static void main17 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        Study08 find = new Study08();
        System.out.print("元素个数:");
        int num = scanner.nextInt();
        int[] arr = new int[num];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        System.out.print("你要找的数值是：");
        int n = scanner.nextInt();
        int key = find.find(arr,n);
        if( -1 == key) {
            System.out.println("没找到");
        } else {
            System.out.println("该数值是："+key);
        }


        scanner.close(); // 关闭IO流，代码好风格;
    }


//    心算能力训练
    public static boolean confirmRetry() {
        Scanner scanner = new Scanner(System.in);
        int cont = 0;
        do{
            System.out.println("再来一次？ <Yes..1 / NO...0>:");
            System.out.print("你的选择是：");
            if(scanner.hasNextInt()) {
                cont = scanner.nextInt();
            }
        }while(cont != 0 && cont !=1 );
        return ( 1 == cont);
    }
    public static void main16 (String[] args) {
        Random  random = new Random();
        Scanner scanner = new Scanner(System.in);
        System.out.println("心算能力训练");
        do{
            int x = random.nextInt(900)+100;
            int y = random.nextInt(900)+100;
            int z = random.nextInt(900)+100;
            System.out.print(x+"+"+y+"+"+z+"=");
            int k = scanner.nextInt();
            if(k == (x+y+z)) {
                System.out.println("恭喜回答正确");
                break;
            }
            System.out.println("回答错误");
        }while(confirmRetry());

    }


//    显示长方形，正方形
    public static void puSquare (int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print("+");
            }
            System.out.println();
        }
    }


    public static void puSquare(int h,int w) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                System.out.print('*');
            }
            System.out.println();
        }
    }


    public static void main15 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        Study08 puSquare = new Study08();
        System.out.println("显示正方形：");
        System.out.print("边长：");
        int a = 0;
        if(scanner.hasNextInt()) {
            a = scanner.nextInt();
        }
        Study08.puSquare(a);
        System.out.println("显示长方形：");
        System.out.print("宽：");
        int h = 0;
        if(scanner.hasNextInt()) {
            h = scanner.nextInt();
        }
        System.out.print("长：");
        int w = 0;
        if(scanner.hasNextInt()) {
            w = scanner.nextInt();
        }
        Study08.puSquare(h,w);
        scanner.close(); // 关闭IO流，代码好风格
    }


//    计算指数幂
    public static double power (double x,int n) {
        double tmp = 1.0;
        for (int i = 0; i < n; i++) {
            tmp *= x;
        }
        return tmp;
    }
    public static void main14 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("计算a的b次幂:");
        System.out.print("实数a:");
        double a = scanner.nextDouble();
        System.out.print("b整数：");
        int b = scanner.nextInt();

        System.out.println(a+"的"+b+"次幂为"+power(a,b));
    }


//    计算3个人的身高，体重，年龄的最大值并显示结果（方法版）
    public static int max (int[] arr) {
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if(max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }
    public static void main13 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] height = new int[3];
        int[] weight = new int[3];
        int[] age = new int[3];
        for (int i = 0; i < age.length; i++) {
            System.out.println("["+(i+1)+"]");
            System.out.print("身高：");
            height[i] = scanner.nextInt();
            System.out.print("体重：");
            weight[i] = scanner.nextInt();
            System.out.print("年龄：");
            age[i] = scanner.nextInt();
        }

        System.out.println("身高的最大值："+max(height)+"cm");
        System.out.println("体重的最大值："+max(weight)+"kg");
        System.out.println("年龄的最大值："+max(age)+"岁");

        scanner.close();
    }




    public static void main12 (String[] args) {
        int[][] arr = new int[][]{{1,2,3},
                                  {4,5,6},};
        int[][] arr2 = new int[][]{{6,3,4},
                                   {5,2,1},};
        int[][] arr3 = new int[2][3];
     /*
      arr3[1][0] = 9;
       System.out.println(arr3[1][0]);
       这样，注意：二维数组中要向指定数组的行列，才可以复制的*/
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr3[i][j] = arr2[i][j] + arr[i][j];
            }
        }
        System.out.println(Arrays.deepToString(arr));
        System.out.println(Arrays.deepToString(arr2));
        System.out.println(Arrays.deepToString(arr3));
    }


//    显示数组变量的值
    public static void main11 (String[] args) {
        int[] arr = new int[5];
        System.out.println("arr ="+arr);
        arr =null;
        System.out.println("arr ="+arr);
        /*我们可以看到显示的并不是数组元素的值，而是数组变量本身的值
        * 是数组声明，使用new 创建的数组的主体与一般的变量不同，它是在程序运行时创建的，因此会被静态分配存储空间
        * 数组主体与一般变量的性质不同，被称为对象
        * 用于指向对象的变量的类型就是引用类型，数组变量的类型是数组类型，这是引用类型的一种*/

    }


//    猜月份
    public static void main10 (String[] args) {
        Random random = new Random();
        Scanner  scanner = new Scanner (System.in);
        String[] mothString = new String[]{"一月","二月","三月","四月","五月","六月",
                                           "七月","八月","九月","十月","十一月","十二月"};
        int moth = random.nextInt(11)+1;
        int n = 0;
        do{
            System.out.print("请输入月份:");
            if(scanner.hasNextInt()) {
                n = scanner.nextInt();
            }
        }while(n != moth);

        System.out.println("你猜对了，是"+mothString[n-1]);
        scanner.close(); // 关闭IO流，代码好风格，
    }


//    复制数组
    public static void main9 (String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int n = 0;
        System.out.print("元素个数:");
        if(scanner.hasNextInt()) {
            n = scanner.nextInt();
            int[] arr = new int[n];
            int[] brr = new int[n];
            for (int i = 0; i < n; i++) {
                 arr[i] = random.nextInt(100);
            }
            int i = 0;
            for (int tmp:arr) {
                brr[i++] = tmp;
//                System.out.println("tmp"+tmp);
            }

            for (int tmp:arr) {
                System.out.print("\t"+tmp);
            }
            System.out.println();
            for (int tmp:brr) {
                System.out.print("\t"+tmp);
            }
        }
        scanner.close(); // 关闭IO流，代码好风格
    }



//    数组的赋值
    public static void main8 (String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        int[] brr = new int[]{6,5,4,3,2,1,0};
        for (int tmp:arr) {
            System.out.print("arr ="+tmp+" ");
        }
        System.out.println();
        for (int tmp:brr) {
            System.out.print("brr ="+tmp+" ");
        }
        System.out.println();
        brr = arr;
        arr[0] = 10;
        for (int tmp:arr) {
            System.out.print("arr ="+tmp+" ");
        }
        System.out.println();
        for (int tmp:brr) {
            System.out.print("brr ="+tmp+" ");
        }
        /*从结果上看其实 brr = arr,
        *               arr[0] = 10
        * 我们可以发现，因为 brr = arr最后的引用类型的赋值，从而导致了，
        *  无论是 brr ,还是 arr 它们都是引用了同一个数组主体*/
    }


    
//    对数组中的元素进行倒序排序并显示
    public static void main7 (String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int n = 0;
        System.out.print("元素个数:");
        if(scanner.hasNextInt()) {
            n = scanner.nextInt();
        }
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(90);
            System.out.print("\tarr["+i+"]="+arr[i]);
        }

        for (int i = 0; i < arr.length / 2; i++) {
            int tmp = arr[i];
            arr[i] = arr[arr.length-i-1];
            arr[arr.length-i-1] = arr [i];
        }
        System.out.println();
        int i = 0;
        for (int tmp:arr) {
            System.out.print("\tarr["+(i++)+"]="+tmp);
        }
        scanner.close(); // 关闭IO流，代码好风格
    }



//    读出正整数所有的约数
    public static void main6 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = 0;
        do{
            System.out.print("请输入正整数:");
            if(scanner.hasNextInt()) {
                x = scanner.nextInt();
            }
        }while(x <= 0);

        for (int i = 1; i <= x; i++) {
            if(x %i == 0) {
                System.out.println(i);
            }
        }
        scanner.close(); // 关闭IO流代码，好风格
    }


//    逆序显示正整数值
    public static void main5 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = 0;
        do{
            System.out.print("请输入正整数值:");
            if(scanner.hasNextInt()) {
                x = scanner.nextInt();
            }

        }while(x <= 0);

        while(x > 0) {
            System.out.print(x%10);
            x /= 10 ;
        }
        scanner.close(); //关闭IO 流代码好风格
    }


//    确定表达式的求值顺序
    public static void main4 (String[] args) {
        int num = 0;
        int x = (num++) *(2+num); // 先算 * 后算后置++，
        System.out.println("num== "+num); // 1
        System.out.println("X ="+x);  // 0
    }



//    倒数
    public static void main3 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        do{
            System.out.print("请输入正整数");
            if(scanner.hasNextInt()) {
                num = scanner.nextInt();
            }
        }while(num <= 0);

        while(num > 0) {
            System.out.println(num);
            num--;
        }
        scanner.close(); // 关闭IO流，代码好风格
    }


    public static void main2 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        System.out.print("整数值:");
        if(scanner.hasNextInt()) {
            num = scanner.nextInt();
        }

        if(!(num != 0)) {
            System.out.println("该数值是:0");
        } else {
            System.out.println("该数值不是0");
        }
        scanner.close(); //关闭IO流，代码好习惯
    }



//    猜数字
    public static void main1 (String[] args) {
        Random rand = new Random();
        Scanner scanner = new Scanner(System.in);
        int n = 0;
        int ran = rand.nextInt(100);
        do{
            System.out.print("请输入你猜的数：");
            if(scanner.hasNextInt()) {
                n = scanner.nextInt();
            }
            if(n > ran) {
                System.out.println(" 你猜大了");
            }else if(n < ran) {
                System.out.println("你猜小了");
            }

        }while(n != ran);
        System.out.println("你猜对了");

        scanner.close(); // 关闭IO流，代码好习惯
    }
}
